package com.chengyu.core.controller.goods;

import com.chengyu.core.annotation.LogControllerAnnotation;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.enums.MethodType;
import com.chengyu.core.model.GoodsSpec;
import com.chengyu.core.request.GoodsSpecRequest;
import com.chengyu.core.request.GoodsSpecSearchRequest;
import com.chengyu.core.request.PageParamRequest;
import com.chengyu.core.result.CommonResult;
import com.chengyu.core.service.goods.GoodsSpecService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 商品规则值(规格)控制器
 */
@Slf4j
@RestController
@RequestMapping("product/spec")
@Api(tags = "商户端商品规则值(规格)控制器") //配合swagger使用
public class GoodRuleController {

    @Autowired
    private GoodsSpecService goodsSpecService;

    @PreAuthorize("hasAuthority('merchant:product:rule:page:list')")
    @ApiOperation(value = "商户端商品规则值(规格)分页列表") //配合swagger使用
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<GoodsSpec>> getList(@Validated GoodsSpecSearchRequest request,
                                                       @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(goodsSpecService.getList(request, pageParamRequest)));
    }

    @PreAuthorize("hasAuthority('merchant:product:rule:save')")
    @LogControllerAnnotation(intoDB = true, methodType = MethodType.ADD, description = "新增商品规格")
    @ApiOperation(value = "新增商品规格")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@Validated GoodsSpecRequest GoodsSpecRequest) {
        if (goodsSpecService.save(GoodsSpecRequest)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PreAuthorize("hasAuthority('merchant:product:rule:delete')")
    @LogControllerAnnotation(intoDB = true, methodType = MethodType.DELETE, description = "删除商品规格")
    @ApiOperation(value = "删除商品规格")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public CommonResult<String> delete(@PathVariable Integer id) {
        if (goodsSpecService.removeById(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PreAuthorize("hasAuthority('merchant:product:rule:update')")
    @LogControllerAnnotation(intoDB = true, methodType = MethodType.UPDATE, description = "修改商品规格")
    @ApiOperation(value = "修改商品规格")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update( @Validated GoodsSpecRequest GoodsSpecRequest) {
        if (goodsSpecService.updateRule(GoodsSpecRequest)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PreAuthorize("hasAuthority('merchant:product:rule:info')")
    @ApiOperation(value = "商品规格详情")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public CommonResult<GoodsSpec> info(@PathVariable Integer id) {
        GoodsSpec GoodsSpec = goodsSpecService.getById(id);
        return CommonResult.success(GoodsSpec);
   }
}



